// var i;
// var $bundleConfig = require("./BundleConfig");
// var $audioManager = require("./AudioManager");
// var $uIManager = require("./UIManager");
// var $uIView = require("./UIView");
// var $commonUtils = require("./CommonUtils");
// var $excel = require("./Excel");
// var $uIConfig_Home = require("./UIConfig_Home");
// var $buffMag = require("./BuffMag");
// var y = cc._decorator;
// var m = y.ccclass;
// var v = y.property;
// var b = y.inspector;
// var w = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.tipView = null;
//         t.scroll = null;
//         t.tipUi = null;
//         t.nowBack = null;
//         t.endindex = -1;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype._show = function (e) {
//         var t = [];
//         for (var o = 1; o < arguments.length; o++) {
//             t[o - 1] = arguments[o];
//         }
//         this.nowBack = e.back;
//         this.tipView.active = !1;
//     };
//     t.prototype.onEnable = function () {
//         this.tipUi.active = !1;
//         this.initUi();
//         $commonUtils.default.cleanGold();
//     };
//     t.prototype.initUi = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var o;
//             var n;
//             var i;
//             var a;
//             var r;
//             var c;
//             var u;
//             var d;
//             var p;
//             var h;
//             var g;
//             var y;
//             var m;
//             var v;
//             var b;
//             return __generator(this, function (s) {
//                 switch (s.label) {
//                     case 0:
//                         for (e = $buffMag.BuffMgr.ciTiaoData, t = {}, c = 0; c < e.length; c++) {
//                             o = e[c];
//                             (n = t[o.id]) ||
//                                 (n = {
//                                     id: o.id
//                                 });
//                             (i = n.count) || (i = 0);
//                             i += 1;
//                             n.count = i;
//                             t[o.id] = n;
//                         }
//                         a = Object.keys(t);
//                         (r = this.scroll.content.children).forEach(function (e) {
//                             e.active = !1;
//                         });
//                         c = 0;
//                         s.label = 1;
//                     case 1:
//                         if (c < a.length) {
//                             return (
//                                 (u = a[c]),
//                                 (d = t[u]),
//                                 (p = r[c]) || ((p = cc.instantiate(r[0])), this.scroll.content.addChild(p)),
//                                 (p.active = !0),
//                                 (h = p.children[1].getComponent(cc.Sprite)),
//                                 (g = p.children[0].getComponent(cc.Sprite)),
//                                 (y = p.children[2].getComponent(cc.Label)),
//                                 (m = $excel.Excel.shuiguocitiao(d.id)),
//                                 [
//                                     4,
//                                     this._loadRes(
//                                         $bundleConfig.BundleNames.Game,
//                                         "res/skill_icon" + m.quality,
//                                         cc.SpriteFrame
//                                     )
//                                 ]
//                             );
//                         } else {
//                             return [3, 5];
//                         }
//                     case 2:
//                         v = s.sent();
//                         g.spriteFrame = v;
//                         return [
//                             4,
//                             this._loadRes(
//                                 $bundleConfig.BundleNames.Common,
//                                 "res/skillicon/skill_" + d.id,
//                                 cc.SpriteFrame
//                             )
//                         ];
//                     case 3:
//                         b = s.sent();
//                         h.spriteFrame = b;
//                         y.string = "×" + d.count;
//                         p.on(cc.Node.EventType.TOUCH_START, this.onClickItem.bind(this, c, d), this);
//                         s.label = 4;
//                     case 4:
//                         c++;
//                         return [3, 1];
//                     case 5:
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.onClickItem = function (e, t) {
//         $audioManager.AudioMgr.playEffect("effect/button");
//         if (this.endindex != e) {
//             this.tipUi.active = !0;
//             this.endindex = e;
//             var o = this.scroll.content.children[e].convertToWorldSpaceAR(cc.v2(0, 0));
//             o = this.node.convertToNodeSpaceAR(o);
//             this.tipUi.y = o.y + 100;
//             this.tipUi.x = o.x;
//             var n = -(this.tipUi.x + cc.winSize.width / 2);
//             var i = this.tipUi.x + cc.winSize.width / 2;
//             if (n < cc.winSize.width / 2) {
//                 this.tipUi.x = -(cc.winSize.width / 2 - this.tipUi.width / 2);
//             }
//             if (i > cc.winSize.width / 2) {
//                 this.tipUi.x = cc.winSize.width / 2 - this.tipUi.width / 2;
//             }
//             this.tipUi.getChildByName("jiantou").x = o.x - this.tipUi.x;
//             var a = $excel.Excel.shuiguocitiao(t.id);
//             var r = this.tipUi.getChildByName("lb").getComponent(cc.RichText);
//             var c = this.tipUi.getChildByName("layout").getChildByName("count").getComponent(cc.Label);
//             var s = $excel.Excel.shuiguobuff(a.citiaoshuzhitype);
//             if (-1 != a.citiaochuxian) {
//                 this.tipUi.getChildByName("layout").active = !0;
//                 c.string = t.count * s.value[0] + "%";
//             } else {
//                 this.tipUi.getChildByName("layout").active = !1;
//             }
//             this.setCiTiaoContent(r, a, t.count);
//         } else {
//             this.tipUi.active = !1;
//             this.endindex = -1;
//         }
//     };
//     t.prototype.showSetting = function () {
//         $uIManager.default.open($bundleConfig.BundleNames.Home, $uIConfig_Home.UIView_Home.UI_Setting);
//     };
//     t.prototype.setCiTiaoContent = function (e, t) {
//         var o = t.citiaomiaoshu;
//         var n = $excel.Excel.shuiguobuff(t.citiaoshuzhitype);
//         var i = (o = $commonUtils.default.replaceSymbols(o, "n", [""])).split("X");
//         var a = "";
//         for (var r = 0; r < i.length; r++) {
//             var c = i[r];
//             if (n.value[r] && r < i.length - 1) {
//                 a += "<color=#ffffff>" + c + "</c><color=#00ff00>" + n.value[r] + "</color>";
//             } else {
//                 a += "<color=#ffffff>" + c + "</c>";
//             }
//         }
//         a = "<outline color=#000000 width = 3>" + a + "</outline>";
//         e.string = a;
//     };
//     t.prototype.onDisable = function () {};
//     t.prototype.onJixu = function () {
//         this.nowBack(3);
//         this._close();
//     };
//     t.prototype.backHome = function () {
//         this.tipView.active = !0;
//     };
//     t.prototype.realHome = function () {
//         this._close();
//         this.nowBack(2);
//     };
//     t.prototype.closeHome = function () {
//         this._close();
//         this.tipView.active = !1;
//     };
//     __decorate([v(cc.Node)], t.prototype, "tipView", void 0);
//     __decorate([v(cc.ScrollView)], t.prototype, "scroll", void 0);
//     __decorate([v(cc.Node)], t.prototype, "tipUi", void 0);
//     return __decorate([m], t);
// })($uIView.default);
// exports.default = w;


// import {BundleNames} from "./BundleConfig";
// import {AudioMgr} from "./AudioManager";
// import UIManager from "./UIManager";
// import UIView from "./UIView";
// import CommonUtils from "./CommonUtils";
// import {Excel} from "./Excel";
// import {BuffMgr} from "./BuffMag";
// import { UIView_Home } from "./UIConfig_Home";


// const { ccclass, property, inspector } = cc._decorator;
// // 这段代码实现了图标提示和一些界面操作
// /**
//  * @class IconTipManager
//  * 建议类名: IconTipManager
//  */

// @ccclass
// export default class UI_GamePuase extends UIView {
//     @property(cc.Node)
//     public tipView: cc.Node | null = null;

//     @property(cc.ScrollView)
//     public scroll: cc.ScrollView | null = null;

//     @property(cc.Node)
//     public tipUi: cc.Node | null = null;

//     private nowBack: Function | null = null;
//     private endindex: number = -1;

//     /**
//      * 显示图标提示
//      * @param e 包含回调函数的对象
//      * 建议方法名: showIconTip
//      */
//     public _show(e: { back: Function }, ...args: any): void {
//         this.nowBack = e.back;
//         this.tipView.active = false;
//     }

//     public onEnable(): void {
//         this.tipUi.active = false;
//         this.initUi();
//         CommonUtils.cleanGold();
//     }

//     /**
//      * 初始化界面
//      * 建议方法名: initializeUI
//      */
//     public async initUi(): Promise<void> {
//         const buffData = BuffMgr.ciTiaoData;
//         const buffCountMap: { [id: string]: { id: string; count: number } } = {};

//         // 统计各个buff的数量
//         for (const buff of buffData) {
//             if (!buffCountMap[buff.id]) {
//                 buffCountMap[buff.id] = { id: buff.id, count: 0 };
//             }
//             buffCountMap[buff.id].count++;
//         }

//         const buffIds = Object.keys(buffCountMap);
//         const children = this.scroll.content.children;
//         children.forEach(child => child.active = false);

//         for (let i = 0; i < buffIds.length; i++) {
//             const buffId = buffIds[i];
//             const buffInfo = buffCountMap[buffId];
//             let childNode = children[i] || cc.instantiate(children[0]);
//             if (!children[i]) {
//                 this.scroll.content.addChild(childNode);
//             }
//             childNode.active = true;

//             const spriteOne = childNode.children[1].getComponent(cc.Sprite);
//             const spriteZero = childNode.children[0].getComponent(cc.Sprite);
//             const label = childNode.children[2].getComponent(cc.Label);
//             const buffIconInfo = Excel.shuiguocitiao(buffInfo.id);

//             const iconOne = await this._loadRes(
//                 BundleNames.Game,
//                 "res/skill_icon" + buffIconInfo.quality,
//                 cc.SpriteFrame
//             );
//             spriteZero.spriteFrame = iconOne as cc.SpriteFrame;

//             const iconZero = await this._loadRes(
//                 BundleNames.Common,
//                 "res/skillicon/skill_" + buffInfo.id,
//                 cc.SpriteFrame
//             );
//             spriteOne.spriteFrame = iconZero as cc.SpriteFrame;

//             label.string = `×${buffInfo.count}`;

//             childNode.on(cc.Node.EventType.TOUCH_START, this.onClickItem.bind(this, i, buffInfo), this);
//         }
//     }

//     /**
//      * 点击图标事件
//      * @param index 图标索引
//      * @param buffInfo buff 信息
//      * 建议方法名: handleIconClick
//      */
//     public onClickItem(index: number, buffInfo: { id: string; count: number }): void {
//         AudioMgr.playEffect("effect/button");
//         if (this.endindex !== index) {
//             this.tipUi.active = true;
//             this.endindex = index;
//             let worldPos = this.scroll.content.children[index].convertToWorldSpaceAR(cc.v2(0, 0));
//             let localPos = this.node.convertToNodeSpaceAR(worldPos);
//             this.tipUi.y = localPos.y + 100;
//             this.tipUi.x = localPos.x;

//             const leftBound = -(this.tipUi.x + cc.winSize.width / 2);
//             const rightBound = this.tipUi.x + cc.winSize.width / 2;

//             if (leftBound < cc.winSize.width / 2) {
//                 this.tipUi.x = -(cc.winSize.width / 2 - this.tipUi.width / 2);
//             }
//             if (rightBound > cc.winSize.width / 2) {
//                 this.tipUi.x = cc.winSize.width / 2 - this.tipUi.width / 2;
//             }

//             this.tipUi.getChildByName("jiantou").x = localPos.x - this.tipUi.x;

//             const buffIconInfo = Excel.shuiguocitiao(buffInfo.id);
//             const richText = this.tipUi.getChildByName("lb").getComponent(cc.RichText);
//             const countLabel = this.tipUi.getChildByName("layout").getChildByName("count").getComponent(cc.Label);
//             const buffEffectInfo = Excel.shuiguobuff(buffIconInfo.citiaoshuzhitype);

//             if (buffIconInfo.citiaochuxian !== -1) {
//                 this.tipUi.getChildByName("layout").active = true;
//                 countLabel.string = buffInfo.count * buffEffectInfo.value[0] + "%";
//             } else {
//                 this.tipUi.getChildByName("layout").active = false;
//             }

//             this.setCiTiaoContent(richText, buffIconInfo, buffInfo.count);
//         } else {
//             this.tipUi.active = false;
//             this.endindex = -1;
//         }
//     }

//     public showSetting(): void {
//         UIManager.open(BundleNames.Home, UIView_Home.UI_Setting);
//     }

//     /**
//      * 设置词条内容
//      * @param richText 文本组件
//      * @param buffIconInfo buff图标信息
//      * @param count 数量
//      * 建议方法名: setBuffContent
//      */
//     public setCiTiaoContent(richText: cc.RichText, buffIconInfo: any, count: number): void {
//         let description = buffIconInfo.citiaomiaoshu;
//         const buffEffectInfo = Excel.shuiguobuff(buffIconInfo.citiaoshuzhitype);
//         description = CommonUtils.replaceSymbols(description, "n", [""]).split("X");
//         let content = "";

//         for (let i = 0; i < description.length; i++) {
//             const part = description[i];
//             if (buffEffectInfo.value[i] && i < description.length - 1) {
//                 content += `<color=#ffffff>${part}</color><color=#00ff00>${buffEffectInfo.value[i]}</color>`;
//             } else {
//                 content += `<color=#ffffff>${part}</color>`;
//             }
//         }

//         richText.string = `<outline color=#000000 width = 3>${content}</outline>`;
//     }

//     public onDisable(): void { }

//     public onJixu(): void {
//         this._close();
//         this.nowBack(3);
//     }

//     public backHome(): void {
//         this.tipView.active = true;
//     }

//     public realHome(): void {
//         this._close();
//         this.nowBack(2);
//     }

//     public closeHome(): void {
//         this._close();
//         this.tipView.active = false;
//     }

//     /**
//      * 加载资源
//      * 建议方法名: loadResource
//      */
//     protected _loadRes(bundleName: string, url: string, type: typeof cc.Asset): Promise<cc.Asset> {
//         return new Promise((resolve, reject) => {
//             cc.assetManager.loadBundle(bundleName, (err, bundle) => {
//                 if (err) {
//                     reject(err);
//                 } else {
//                     bundle.load(url, type, (err, asset) => {
//                         if (err) {
//                             reject(err);
//                         } else {
//                             resolve(asset);
//                         }
//                     });
//                 }
//             });
//         });
//     }

//     // /**
//     //  * 关闭界面
//     //  * 建议方法名: closeUI
//     //  */
//     // protected _close(): void {
//     //     // 具体关闭界面逻辑
//     // }
// }

import { BundleNames } from "./BundleConfig";
import { AudioMgr } from "./AudioManager";
import UIManager from "./UIManager";
import UIViewBase from "./UIView";
import CommonUtils from "./CommonUtils";
import { Excel } from "./Excel";
import { BuffMgr } from "./BuffMag";
import { UIView_Home } from "./UIConfig_Home";

const { ccclass, property } = cc._decorator;

/**
 * @class IconTipManager
 * 建议类名: IconTipManager
 */
@ccclass
export default class ViewGamePuase extends UIViewBase {
    @property(cc.Node)
    public tipView: cc.Node | null = null;

    @property(cc.ScrollView)
    public scroll: cc.ScrollView | null = null;

    @property(cc.Node)
    public tipUi: cc.Node | null = null;

    private nowBack: Function | null = null;
    private endindex: number = -1;

    /**
     * 显示图标提示
     * @param e 包含回调函数的对象
     * 建议方法名: showIconTip
     */
    public _show(e: { back: Function }, ...args: any): void {
        this.nowBack = e.back;
        this.tipView.active = false;
    }

    public onEnable(): void {
        this.tipUi.active = false;
        this.initUI();
        CommonUtils.clearCoins();
    }

    /**
     * 初始化界面
     * 建议方法名: initializeUI
     */
    public async initUI(): Promise<void> {
        const buffData = BuffMgr.ciTiaoData;
        const buffCountMap: { [buffId: string]: { id: string; count: number } } = {};

        // 统计各个buff的数量
        for (const buff of buffData) {
            if (!buffCountMap[buff.id]) {
                buffCountMap[buff.id] = { id: buff.id, count: 0 };
            }
            buffCountMap[buff.id].count++;
        }

        const buffIds = Object.keys(buffCountMap);
        const children = this.scroll.content.children;
        children.forEach(child => child.active = false);

        for (let i = 0; i < buffIds.length; i++) {
            const buffId = buffIds[i];
            const buffInfo = buffCountMap[buffId];
            let childNode = children[i] || cc.instantiate(children[0]);
            if (!children[i]) {
                this.scroll.content.addChild(childNode);
            }
            childNode.active = true;

            const spriteOne = childNode.children[1].getComponent(cc.Sprite);
            const spriteZero = childNode.children[0].getComponent(cc.Sprite);
            const label = childNode.children[2].getComponent(cc.Label);
            const buffIconInfo = Excel.shuiguocitiao(buffInfo.id);

            const iconSpriteZero = await this._loadRes(
                BundleNames.Game,
                "res/skill_icon" + buffIconInfo.quality,
                cc.SpriteFrame
            );
            spriteZero.spriteFrame = iconSpriteZero as cc.SpriteFrame;

            const iconSpriteOne = await this._loadRes(
                BundleNames.Common,
                "res/skillicon/skill_" + buffInfo.id,
                cc.SpriteFrame
            );
            spriteOne.spriteFrame = iconSpriteOne as cc.SpriteFrame;

            label.string = `×${buffInfo.count}`;

            childNode.on(cc.Node.EventType.TOUCH_START, this.handleIconClick.bind(this, i, buffInfo), this);
        }
    }

    /**
     * 点击图标事件
     * @param index 图标索引
     * @param buffInfo buff 信息
     * 建议方法名: handleIconClick
     */
    public handleIconClick(index: number, buffInfo: { id: string; count: number }): void {
        AudioMgr.playSoundEffect("effect/button");
        if (this.endindex !== index) {
            this.tipUi.active = true;
            this.endindex = index;
            const worldPos = this.scroll.content.children[index].convertToWorldSpaceAR(cc.v2(0, 0));
            const localPos = this.node.convertToNodeSpaceAR(worldPos);
            this.tipUi.y = localPos.y + 100;
            this.tipUi.x = localPos.x;

            const leftBound = -(this.tipUi.x + cc.winSize.width / 2);
            const rightBound = this.tipUi.x + cc.winSize.width / 2;

            if (leftBound < cc.winSize.width / 2) {
                this.tipUi.x = -(cc.winSize.width / 2 - this.tipUi.width / 2);
            }
            if (rightBound > cc.winSize.width / 2) {
                this.tipUi.x = cc.winSize.width / 2 - this.tipUi.width / 2;
            }

            this.tipUi.getChildByName("jiantou").x = localPos.x - this.tipUi.x;

            const buffIconInfo = Excel.shuiguocitiao(buffInfo.id);
            const richText = this.tipUi.getChildByName("lb").getComponent(cc.RichText);
            const countLabel = this.tipUi.getChildByName("layout").getChildByName("count").getComponent(cc.Label);
            const buffEffectInfo = Excel.shuiguobuff(buffIconInfo.citiaoshuzhitype);

            if (buffIconInfo.citiaochuxian !== -1) {
                this.tipUi.getChildByName("layout").active = true;
                countLabel.string = buffInfo.count * buffEffectInfo.value[0] + "%";
            } else {
                this.tipUi.getChildByName("layout").active = false;
            }

            this.setBuffContent(richText, buffIconInfo, buffInfo.count);
        } else {
            this.tipUi.active = false;
            this.endindex = -1;
        }
    }

    public showSetting(): void {
        UIManager.open(BundleNames.Home, UIView_Home.UI_Setting);
    }

    /**
     * 设置词条内容
     * @param richText 文本组件
     * @param buffIconInfo buff图标信息
     * @param count 数量
     * 建议方法名: setBuffContent
     */
    public setBuffContent(richText: cc.RichText, buffIconInfo: any, count: number): void {
        let description = buffIconInfo.citiaomiaoshu;
        const buffEffectInfo = Excel.shuiguobuff(buffIconInfo.citiaoshuzhitype);
        description = CommonUtils.replaceStringSymbols(description, "n", [""]).split("X");
        let content = "";

        for (let i = 0; i < description.length; i++) {
            const part = description[i];
            if (buffEffectInfo.value[i] && i < description.length - 1) {
                content += `<color=#ffffff>${part}</color><color=#00ff00>${buffEffectInfo.value[i]}</color>`;
            } else {
                content += `<color=#ffffff>${part}</color>`;
            }
        }

        richText.string = `<outline color=#000000 width = 3>${content}</outline>`;
    }

    public onDisable(): void { }

    public onJixu(): void {
        this._close();
        this.nowBack && this.nowBack(3);
    }

    public backHome(): void {
        this.tipView.active = true;
    }

    public realHome(): void {
        this._close();
        this.nowBack && this.nowBack(2);
    }

    public closeHome(): void {
        this._close();
        this.tipView.active = false;
    }

    /**
     * 加载资源
     * 建议方法名: loadResource
     */
    protected _loadRes(bundleName: string, url: string, type: typeof cc.Asset): Promise<cc.Asset> {
        return new Promise((resolve, reject) => {
            cc.assetManager.loadBundle(bundleName, (err, bundle) => {
                if (err) {
                    reject(err);
                } else {
                    bundle.load(url, type, (err, asset) => {
                        if (err) {
                            reject(err);
                        } else {
                            resolve(asset);
                        }
                    });
                }
            });
        });
    }
}